package com.ruoyi.system.utils;


import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.*;

public class BeanUtil {

    public BeanUtil() {
    }

    public static List getMapList(List beanList) {
        if (beanList == null) {
            return null;
        } else {
            ArrayList lstMap = new ArrayList();

            try {
                Iterator iter = beanList.iterator();

                while(iter.hasNext()) {
                    Object obj = iter.next();
                    Map map = transBean2Map(obj);
                    lstMap.add(map);
                }
            } catch (Exception var5) {
                var5.printStackTrace();
            }

            return lstMap;
        }
    }

    public static HashMap<String, Object> transBean2Map(Object obj) {
        if (obj == null) {
            return null;
        } else {
            HashMap map = new HashMap();

            try {
                BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
                PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
                PropertyDescriptor[] arr$ = propertyDescriptors;
                int len$ = propertyDescriptors.length;

                for(int i$ = 0; i$ < len$; ++i$) {
                    PropertyDescriptor property = arr$[i$];
                    String key = property.getName();
                    if (!key.equals("class")) {
                        Method getter = property.getReadMethod();
                        Object value = getter.invoke(obj);
                        map.put(key, value);
                    }
                }
            } catch (Exception var11) {
                System.out.println("transBean2Map Error " + var11);
            }

            return map;
        }
    }

    public static void transMap2Bean(Map<String, Object> map, Object obj) {
        String printkey = "";

        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            PropertyDescriptor[] arr$ = propertyDescriptors;
            int len$ = propertyDescriptors.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                PropertyDescriptor property = arr$[i$];
                String key = property.getName();
                if (map.containsKey(key)) {
                    Object value = map.get(key);
                    Method setter = property.getWriteMethod();
                    setter.invoke(obj, value);
                }
            }
        } catch (Exception var12) {
            System.out.println("====================" + printkey);
            System.out.println("transMap2Bean Error " + var12);
        }

    }

    /**
     * Bean Copy
     * @param source    数据源（数据提供方）
     * @param target    目标类（数据接收方）
     */
    public static void copyProperties(Object source, Object target) {
        BeanUtils.copyProperties(source, target);
    }

    /**
     * Bean Cpy
     * @param source    数据源
     * @param clazz     返回的对象
     * @return
     * @param <T>
     */
    public static <T> T copyProperties(Object source, Class clazz) {
        Object result = null;
        try {
            result = clazz.newInstance();
            BeanUtils.copyProperties(source, result);
        } catch (Exception e) {

        }
        return (T) result;
    }

    /**
     * Bean Copy 已经有值的属性，不进行覆盖
     * @param source    数据源（数据提供方）
     * @param target    目标类（数据接收方）
     */
    public static void copyPropertiesIgnoreValuableProperties(Object source, Object target) {
        try {

            HashMap<String, Object> sourceMap = transBean2Map(source);

            // 存储需要覆盖的属性名称
            List<String> mapKey = new ArrayList<>();

            HashMap<String, Object> targetMap = transBean2Map(target);
            for (Map.Entry<String, Object> entry : targetMap.entrySet()) {
                if (!ObjectUtils.isEmpty(entry.getValue())) {
                    continue;
                }
                if (!sourceMap.containsKey(entry.getKey())) {
                    continue;
                }
                if (ObjectUtils.isEmpty(sourceMap.get(entry.getKey()))) {
                    continue;
                }
                mapKey.add(entry.getKey());
            }
            if (ObjectUtils.isEmpty(mapKey)) {
                return;
            }

            for (String key : mapKey) {
                targetMap.put(key, sourceMap.get(key));
            }

            // 新的Map转换成Bean
            transMap2Bean(targetMap, target);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copyListProperties(List source, List target, Class clazz) {
        try {
            for (Object item : source) {
                Object newInstance = clazz.newInstance();
                BeanUtils.copyProperties(item, newInstance);
                target.add(newInstance);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Bean Copy
     * @param source    数据源（数据提供方）
     * @param target    目标类（数据接收方）
     * @param ignoreProperties  需要忽略复制的的字段名
     */
    public static void copyProperties(Object source, Object target, String... ignoreProperties) {
        try {
            BeanUtils.copyProperties(source, target, ignoreProperties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static PageInfo copyPageInfo(PageInfo pageInfo, Class clazz) {

        List list = new ArrayList();
        copyListProperties(pageInfo.getList(), list, clazz);

        PageInfo resPageInfo = new PageInfo<>();
        resPageInfo.setPageNum(pageInfo.getPageNum());
        resPageInfo.setPageSize(pageInfo.getPageSize());
        resPageInfo.setTotal(pageInfo.getTotal());
        resPageInfo.setList(list);
        return resPageInfo;
    }


    /**
     * 复制PageInfo基本信息
     * @param pageInfo
     */
    public static PageInfo copyPageNum(PageInfo pageInfo) {
        PageInfo resPageInfo = new PageInfo();
        resPageInfo.setPageNum(pageInfo.getPageNum());
        resPageInfo.setPageSize(pageInfo.getPageSize());
        resPageInfo.setTotal(pageInfo.getTotal());
        resPageInfo.setList(Collections.emptyList());
        return resPageInfo;
    }
}
